package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    CheckGroupMapper checkGroupMapper;

    /**
     * 添加检查组
     * @param checkGroup
     * @param checkitemIds
     */
    @Override
    public void addCheckGroup(CheckGroup checkGroup, List<Integer> checkitemIds) {
        checkGroupMapper.addCheckGroup(checkGroup);
        checkGroupMapper.addCheckGroupAndCheckItem(checkGroup.getCode(), checkitemIds);
    }

    /**
     * 删除检查组
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        checkGroupMapper.deleteCheckItemAndCheckGroup(id);
        checkGroupMapper.deleteById(id);
    }

    /**
     * 根据套餐id查询关联的检查项id
     * @param setmealId
     * @return
     */
    @Override
    public List<Integer> findCheckGroupIdsBySetmealId(Integer setmealId) {
        return checkGroupMapper.findCheckGroupIdsBySetmealId(setmealId);
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        List<CheckGroup> list = checkGroupMapper.pageList(currentPage, pageSize, queryString);
        PageInfo<CheckGroup> pageInfo = new PageInfo<>(list);
        return new PageResult(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 更新检查组
     * @param checkGroup
     * @param checkitemIds
     */
    @Override
    public void update(CheckGroup checkGroup, List<Integer> checkitemIds) {
        checkGroupMapper.deleteCheckItemAndCheckGroup(checkGroup.getId());
        checkGroupMapper.deleteById(checkGroup.getId());
        checkGroupMapper.addCheckGroup(checkGroup);
        checkGroupMapper.addCheckGroupAndCheckItem(checkGroup.getCode(), checkitemIds);
    }

    /**
     * 查询所有检查组
     * @return
     */
    @Override
    public List<CheckGroup> findAll() {
        return checkGroupMapper.findAll();
    }

    /**
     * 根据id查询检查组
     * @param id
     * @return
     */
    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupMapper.findById(id);
    }

}
